<?php
use Workerman\Worker;
use Workerman\Lib\Timer;
use Beanstalk\Client;
require_once __DIR__ . '/Workerman/Autoloader.php';
require_once __DIR__ . '/Channel/src/Server.php';
require_once __DIR__ . '/Channel/src/Client.php';
require_once __DIR__ . '/mysql/src/Connection.php';  //mysql组件
require_once __DIR__ . '/Beanstalk/Client.php';

// 创建一个Worker监听2345端口，使用http协议通讯
$worker = new Worker("websocket://0.0.0.0:9098");
// 心跳间隔25秒
define('HEARTBEAT_TIME', 30);
// 设置实例的名称
$worker->name = 'console';
// 启动4个进程对外提供服务
$worker->count = 10;

$worker->onWorkerStart = function($worker) {
    
};
//连接时候进行处理
$worker->onConnect = function($connection)use($worker)
{
    $msg = "workerID:{$worker->id} connectionID:{$connection->id} connected\n";
    echo $msg;
    $connection->send($msg);
};

// 接收到浏览器发送的数据时回复hello world给浏览器
$worker->onMessage = function($connection, $data){
    $worker = $connection->worker;
    $worker_id = $connection->worker->id;
    $connection_id = $connection->id;
    $data=json_decode($data,true);
    if($data['action']=='db'){
        Timer::add(1, function()use($worker,$db){
            foreach($worker->connections as $connection) {
                $connection->send('ssssssss');
            }
        });
    }
};

$worker->onClose = function($connection)
{
    echo "connection closed\n";
};

Worker::runAll();

